package com.example.module_parking.ui.edit_parking
import android.view.View
import android.widget.AdapterView
import android.widget.ArrayAdapter
import android.widget.ImageView
import android.widget.Spinner
import androidx.activity.viewModels
import com.alibaba.android.arouter.facade.annotation.Autowired
import com.alibaba.android.arouter.facade.annotation.Route
import com.example.lib_common.base.BaseActivity
import com.example.lib_common.utils.ToastUtils
import com.example.module_parking.R
import com.example.module_parking.databinding.ActivityEditParkingBinding
import com.example.module_parking.model.AddParkingInfo
import com.example.module_parking.model.ParkingEntity
import com.example.module_parking.ui.ParkingViewModel
import com.google.gson.Gson
import dagger.hilt.android.AndroidEntryPoint


@AndroidEntryPoint
@Route(path = "/module_parking/EditParkingActivity")
class EditParkingActivity :BaseActivity<ParkingViewModel,ActivityEditParkingBinding>() {
    @Autowired(name = "entity")
    @JvmField
    var entity: String? = null

    private lateinit var parkingEntity: ParkingEntity

    private  var isSelected=""
    private  var area=""
    private  var type=""
    private var back: ImageView? = null



    private val mViewModel:ParkingViewModel by viewModels()

    override val mLayoutResId: Int
        get() = R.layout.activity_edit_parking


    override fun initView() {
        // 在此处解析JSON，确保ARouter已注入entityJson
        parkingEntity = Gson().fromJson(entity, ParkingEntity::class.java) ?: run {
            ToastUtils.showLong(this, "数据传递错误")
            finish()
            return
        }
        area=parkingEntity.areaName
        mBinding.etCarBh.setText(parkingEntity.licensePlate)
        mBinding.etBianhao.setText(parkingEntity.spaceNumber)
        type=parkingEntity.spaceType
        mBinding.etUse.setText(parkingEntity.userName)
        mBinding.etBeizhu.setText(parkingEntity.remarks)
        if (parkingEntity.spaceStatus=="空闲"){
            mBinding.radioGroup.check(R.id.status1); // 默认选中第一个选项
        }else if (parkingEntity.spaceStatus=="已占用"){
            mBinding.radioGroup.check(R.id.status2); // 默认选中第二个选项
        }else if (parkingEntity.spaceStatus=="维修中"){
            mBinding.radioGroup.check(R.id.status3); // 默认选中第三个选项

        }



        mBinding.store.setOnClickListener {
            val addParkingInfo = AddParkingInfo(
                areaId = 1,//所属区域ID,示例值(1)
                areaName = area,//所属区域名称,示例值(A园区C栋)
                id = parkingEntity.id,
                licensePlate = mBinding.etCarBh.text.toString() ,//车牌号,示例值(津ABBBBB)
                remarks = mBinding.etBeizhu.text.toString(),//备注
                spaceNumber = mBinding.etBianhao.text.toString(),//车位编号
                spaceStatus = isSelected,//车位状态（空闲、已占用、维修中 通过字典表配置）,示例值(空闲)
                spaceType = type,//车位类型（通过字典表配置）,示例值(标准车位)
                userId = 1,//使用人用户ID,示例值(10)
                userName = mBinding.etUse.text.toString()//使用人用户名称,示例值(小红)
            )
            mViewModel.editParking(parkingEntity.id, addParkingInfo)
        }



/*
 * Spinner,下拉框1
 */
            val spinner1: Spinner = findViewById(R.id.spinner1)
            val options = arrayOf("A园区A栋", "A园区B栋", "A园区C栋")
            val adapter = ArrayAdapter(this, android.R.layout.simple_spinner_item, options).apply {
                setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item)
            }
            spinner1.adapter = adapter

// 3. 监听选择事件
            spinner1.onItemSelectedListener = object : AdapterView.OnItemSelectedListener {
                override fun onItemSelected(parent: AdapterView<*>?, view: View?, position: Int, id: Long) {
                    area=options[position]
                }
                override fun onNothingSelected(parent: AdapterView<*>?) {}
            }

        /*
                * Spinner,下拉框2
                */
            val spinner2: Spinner = findViewById(R.id.spinner2)
            val options2 = arrayOf("标准车位", "A级车位", "B级车位")
            val adapter2 = ArrayAdapter(this, android.R.layout.simple_spinner_item, options2).apply {
                setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item)
            }
            spinner2.adapter = adapter2

// 3. 监听选择事件
            spinner2.onItemSelectedListener = object : AdapterView.OnItemSelectedListener {
                override fun onItemSelected(parent: AdapterView<*>?, view: View?, position: Int, id: Long) {
                    type=options2[position]
                }
                override fun onNothingSelected(parent: AdapterView<*>?) {}
            }


       /*
        * RadioGroup,车位状态
        */
        mBinding.radioGroup.setOnCheckedChangeListener { group, checkedId ->
            when (checkedId) {
                R.id.status1 -> {
                    // 选项1被选中
                    isSelected="空闲"
                }
                R.id.status2 -> {
                    // 选项2被选中
                    isSelected="已占用"
                }
                R.id.status3 -> {
                    // 选项3被选中
                    isSelected="维护中"
                }
            }
        };
        back=mBinding.topBar.findViewById(R.id.back)
        back!!.setOnClickListener {
            finish()
        }

    }

    override fun initData() {

        mViewModel.editParkingLiveData.observe(this){

            if (it==null) {
                ToastUtils.showLong(this,  "没有数据") // 显示空状态
            } else {
                ToastUtils.showLong(this,"保存成功")
                finish()
            }

        }

    }

}